/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
 bool fun(int x,int y){
    return x>y;
 }
 int dfs(TreeNode* node,vector<int>& vv)
 {
    if(node == nullptr)  return 0;
    int l = dfs(node->left,vv),r = dfs(node->right,vv);
    if(l < 0 || r < 0 || r != l)  return -1;
    vv.push_back(l+r+1);
    return l + r + 1;
 }
class Solution {
public:
    int kthLargestPerfectSubtree(TreeNode* root, int k) {
        vector<int> vv;
        dfs(root,vv);
        sort(vv.begin(),vv.end(),fun);
        for(auto e : vv) cout<<e<<' ';
        if(k > vv.size()){
            return -1;
        }
        return vv[k - 1];
    }
};
class T {
public:
    bool operator()(pair<int, int>& p1, pair<int, int> p2) {
        if (p1.second == p2.second)
            return p1.first < p2.first;
        return p1.second < p2.second;
    }
};
class Solution {
public:
    vector<int> findXSum(vector<int>& nums, int k, int x) {
        int size = nums.size();

        vector<int> ret(size - k + 1);
        for (int i = 0; i + k <= size; i++) {
            vector<int> vv(60);

            priority_queue<pair<int, int>, vector<pair<int, int>>, T> pq;
            for (int j = i; j < i + k; j++) {
                vv[nums[j]]++;
            }
            int tmp = x;
            for (int j = 0; j < 60; j++)if(vv[j]!=0) pq.push(make_pair(j, vv[j]));
            while (!pq.empty() && tmp--) {
                ret[i] += (pq.top().first * pq.top().second);
                pq.pop();
            }
        }
        return ret;
    }
};